package dup.login.demo.queue;

import dup.login.demo.config.RabbitConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class RabbitProducer {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void stringSend() {
//        Date date = new Date();
//        String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
//        System.out.println("[dup1] send msg:" + dateString);
//        // 第一个参数为刚刚定义的队列名称
//        this.rabbitTemplate.convertAndSend("dup1", dateString);

        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("order_id", "100");
//        this.rabbitTemplate.convertAndSend("dup1", params);
        rabbitTemplate.convertAndSend(RabbitConfig.ORDER_DELAY_EXCHANGE, RabbitConfig.ORDER_DELAY_ROUTING_KEY, params, message -> {
            // 如果配置了 params.put("x-message-ttl", 5 * 1000); 那么这一句也可以省略,具体根据业务需要是声明 Queue 的时候就指定好延迟时间还是在发送自己控制时间
            message.getMessageProperties().setExpiration(1 * 1000 * 5 + "");
            Date date = new Date();
            String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            System.out.println("send msg date:" + dateString);
            return message;
        });
    }
}
